package design.patterns.creational.prototype.v3;

import design.patterns.creational.prototype.Computer;

import java.io.*;
import java.util.Date;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2019/4/1 10:16
 */

/**
 * 通过序列化和反序列化实现深复制
 */
public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Computer computer = new Computer();
        Date date = new Date();
        computer.setName("联想拯救者");
        computer.setPrice(5000D);
        computer.setDate(date);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(computer);

        byte[] bytes = bos.toByteArray();

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);
        Computer tempComputer = (Computer) ois.readObject();
        System.out.println(tempComputer);
        date.setTime(200);
        System.out.println(tempComputer);
    }
}
